﻿
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace Trig_AMJ_IT_Forms.Layouts
{
    public partial class E_Mail_ID_Request_Form : LayoutsPageBase
    {

        SPHelper obj = new SPHelper();

        #region "Properties"
        string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;
        string ErrorMsgs;

        public static string SiteURL { get; set; }
        public static string EmailReqNo { get; set; }
        public static string MainLstName { get; set; }
        public static string TaskLstName { get; set; }
        public static string CmntsLstName { get; set; }
        public static string lstGuid { get; set; }
        public string camlQry { get; set; }

        public static string AttLibName;
        public static string WorkflowConfigList;
        public static string Mode { get; set; }
        public static string RefNo;


        string WFCurrentOrder = string.Empty;
        string WFTaskType = string.Empty;
        public static string DocumentTypesList { get; set; }

        public static string ReqEmpDivision;
        public static string ReqEmpDept;

        Demand_Voucher_FormClass demandVoucherclass = new Demand_Voucher_FormClass(); // Helper class for this solution
        Trig_WF trigWFClass = new Trig_WF();

        string[] buttonVisibilty = new string[5];

        Label EmpName; Label EmpID; Label Division; Label Designation; Label MobNo;

        #endregion

        public E_Mail_ID_Request_Form()
        {
            SiteURL = SPContext.Current.Site.Url;
            SiteURL = SiteURL + "/sites/forms/";
            MainLstName = "EMail ID Request Forms";
            TaskLstName = "EMail ID Request Tasks";
            CmntsLstName = "EMail ID Request Comments";
            AttLibName = "Attachments";
            WorkflowConfigList = "Workflow Configuration List";
            DocumentTypesList = "DocumentTypes";
        }

        #region "User control manipulation"
        /// <summary>
        /// Get user control controls using find control method 
        /// </summary>
        private void GetUserControls()
        {

            EmpName = userInfo.FindControl("lblEmpName") as Label;
            EmpID = userInfo.FindControl("lblEmpID") as Label;
            Division = userInfo.FindControl("lblDivision") as Label;
            Designation = userInfo.FindControl("lblDesignation") as Label;
            MobNo = userInfo.FindControl("lblMobNo") as Label;
        }

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            txtPassWord.Attributes.Add("onfocus", "this.type='text';");
            txtPassWord.Attributes.Add("onblur", "this.type='password';");
            txtPassWord.Attributes.Add("onChange", "validatePassword();");
            txtUserName.Attributes.Add("onChange", "ckUserName();");
            txtUserNameSuggestion.Attributes.Add("onChange", "ckUserName();");
            txtemailAdd.Attributes.Add("onChange", "validateEmail();");


            Mode = Request.QueryString["Mode"];
            dtpDate.SelectedDate = DateTime.Today;
            GetUserControls();

            if (!IsPostBack)
            {
                RefNo = Convert.ToString(Request.Params["RefID"]);
                ImgUser.ImageUrl = demandVoucherclass.GetUserProfilePictureURL(CurrentUser);
                LblCommentsUserName.Text = CurrentUser;

                SPSecurity.RunWithElevatedPrivileges(delegate() { demandVoucherclass.GetDivisionsMaster(SiteURL, ddlDivision); });
                ddlDivision.Items.Insert(0, "Select");



                if (Mode == "Edit" || Mode == "View")
                {
                    FormApprovalLoad(); if (Mode == "View") InvisibleButtons();
                }
                else if (Mode == "New")
                {
                    FirstLoadofForm();
                }
                else
                {
                    pnlFullForm.Visible = false;
                    pnlAccessDenied.Visible = true;
                    lblAccessDeniedMsg.Visible = true;
                    lblAccessDeniedMsg.Text = "Unable to open the form. Value is missing. Contact administrator";
                }
            }

            btnSubmit.Click += new EventHandler(btnSubmit_Click);
            btnReject.Click += new EventHandler(btnReject_Click);
            btnApprove.Click += new EventHandler(btnApprove_Click);
            rtbForward.CheckedChanged += new EventHandler(rtbForward_CheckedChanged);
            rtbSendBack.CheckedChanged += new EventHandler(rtbSendBack_CheckedChanged);
            rtbForwardOthers.CheckedChanged += new EventHandler(rtbForwardOthers_CheckedChanged);
            

        }

        protected void ddlDivision_SelectedIndexChanged(object sender, EventArgs e)
        {
            ReqEmpDivision = ddlDivision.SelectedItem.Text;
            SPSecurity.RunWithElevatedPrivileges(delegate() { demandVoucherclass.GetDepartmentMaster(SiteURL, ddlDepartment, ReqEmpDivision); });
            ddlDepartment.Items.Insert(0, "Select");
        }


        private void InvisibleButtons()
        {
            btnCntrls.Visible = false;
            pnlView.Visible = true;
            btnSubmit.Visible = false;
            btnApprove.Visible = false;
            btnReject.Visible = false;
            pnlForward.Visible = false;
            pnlCommentArea.Visible = false;
            upAtt.Visible = false;
            btnCancel.Value = "Close";
        }


        public void FirstLoadofForm()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    string[] getDocRefNumber = demandVoucherclass.getRefNum(SiteURL, DocumentTypesList, MainLstName);
                    if (string.IsNullOrEmpty(getDocRefNumber[0]) && string.IsNullOrEmpty(getDocRefNumber[1]))
                    {
                        pnlFullForm.Visible = false;
                        pnlAccessDenied.Visible = true;
                        lblAccessDeniedMsg.Visible = true;
                        lblAccessDeniedMsg.Text = "Access Denied, DocPrefix or Starting Number column value in Document Types Master list is missing. Contact administrator.";
                    }

                });
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_IT_Forms :: E-Mail_ID_Request_Form" + " :: " + "Parent" + " :: " + "FirstLoadofForm";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }

        #region "Submit Forms"

        /// <summary>
        /// Submit the form 
        /// Generate IT Helpdesk Ticket No generation
        /// Add values to E-Mail ID Request Form
        /// Add values to E-Mail ID Request Form Tasks List 
        /// Add values to Comment List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (Request.QueryString["Mode"] == "Edit")
                {
                    string formID = null;// Request.QueryString["FormID"];
                    string refNo = Request.QueryString["RefID"];
                    string taskItemID = Request.QueryString["TaskItemID"];
                    string Approver = SPContext.Current.Web.CurrentUser.LoginName;

                    if (GetSendbackStatus())
                    {
                        if (CheckServersideValidation())
                        {
                            SPLongOperation lop = new SPLongOperation(this.Page);
                            lop.LeadingHTML = "<DIV><H4>E-Mail Request form approval is in progress</H4></DIV>";
                            lop.TrailingHTML = "<DIV><H5>Please wait...</H5></DIV>";
                            lop.Begin();
                            obj.ApprovetheForm(SiteURL, MainLstName, TaskLstName, WorkflowConfigList, CmntsLstName, AttLibName, formID, refNo, taskItemID, WFTaskType, Approver,
                                TxtComments.Text, upfile, rtbForwardOthers, rtbSendBack, pplcnt, false);
                            lop.End(SPContext.Current.Site.Url, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, "");
                        }
                    }
                    else
                    {
                        lblError.Text = "Can't be send back. User is not available";
                    }
                }
                else if (Request.QueryString["Mode"] == "New")
                {
                    SaveFormDetails();
                }
            }

        }

        #endregion


        /// <summary>
        /// check the user availability
        /// </summary>
        /// <returns></returns>

        public bool GetSendbackStatus()
        {
            bool status = true;
            if (rtbSendBack.Checked)
            {
                SPFieldUserValue ofldUser = demandVoucherclass.getSendBackUserfNametoAssignLabelText(SiteURL, TaskLstName, Convert.ToInt16(Request.Params["TaskItemID"]), lblEmailReqNo.Text, rtbSendBack, rtbForward, lblError);
                if (ofldUser == null)
                {
                    status = false;
                }
            }
            return status;
        }


        /// <summary>
        /// Check people editor validations
        /// </summary>
        /// <returns></returns>

        public bool CheckServersideValidation()
        {
            bool rval = true;
            if (rtbForwardOthers.Checked)
            {
                if (pplcnt.Accounts.Count > 0)
                {
                    rval = true;
                }
                else
                {
                    rval = false;
                    lblError.Text = "Please enter the user name to forward";
                }
            }
            return rval;
        }

        /// <summary>
        /// Save Form details 
        /// Add Items in Main List
        /// Add Items in Task List
        /// Add Items in Comments List
        /// Add Attachment to Attachment Library
        /// </summary>

        private void SaveFormDetails()
        {
            SPLongOperation lop = new SPLongOperation(this.Page);
            lop.LeadingHTML = "<DIV><H4>E-Mail Request form submission is in progress...</H4></DIV>";
            lop.TrailingHTML = "<DIV><H5>Please wait...</H5></DIV>";

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                obj.AutoNoGeneration(MainLstName, DocumentTypesList, lblEmailReqNo, SiteURL);
                EmailReqNo = Convert.ToString(lblEmailReqNo.Text).Trim();
                if (EmailReqNo.Contains(DateTime.Now.Year.ToString()))
                {
                    lop.Begin();

                    lstGuid = obj.AddItems(SiteURL, MainLstName, TaskLstName, CmntsLstName, AttLibName, upfile, WorkflowConfigList, pplcnt, lblEmailReqNo.Text,
                        Convert.ToString(dtpDate.SelectedDate), CurrentUser, EmpID.Text, Division.Text,
                             Designation.Text, "", txtFirstName.Text, txtLastName.Text, ddlDepartment.SelectedItem.Text, txtMailUserMobNo.Text, txtUserNameSuggestion.Text, TxtComments.Text, ddlDivision.SelectedItem.Text);
                    lop.End(SPContext.Current.Site.Url, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, "");
                }
                else
                {
                    lblError.Text = "Master list details not in Document Types"; lblError.ForeColor = System.Drawing.Color.Red;
                }
            });
            //lop.End(SPContext.Current.Site.Url, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, "");
        }


        /// <summary>
        /// Get Approver user from task list and check their properties from Workflow Configuration List
        /// Based upon the properties the buttons and radio button should be shown in the screen.
        /// Also bind Submitted details from Master List
        /// </summary>
        public void FormApprovalLoad()
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    string TaskItemID = Convert.ToString(Request.QueryString["TaskItemID"]);
                    if (!string.IsNullOrEmpty(TaskItemID))
                    {
                        using (SPSite FrmSite = new SPSite(SiteURL))
                        {
                            using (SPWeb frmweb = FrmSite.OpenWeb())
                            {
                                SPQuery updatecamQ = new SPQuery();
                                updatecamQ.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + TaskItemID + "</Value></Eq></Where>";
                                if (frmweb.Lists.TryGetList(TaskLstName) != null)
                                {
                                    SPListItemCollection oitemcoll = frmweb.Lists.TryGetList(TaskLstName).GetItems(updatecamQ);
                                    if (oitemcoll.Count > 0)
                                    {
                                        SPListItem oitm = oitemcoll[0];
                                        WFCurrentOrder = Convert.ToString(oitm["WFCurrentOrder"]);
                                        WFTaskType = Convert.ToString(oitm["WFTaskType"]);
                                        if (WFTaskType == "Dynamic")
                                        {
                                            pnlForward.Visible = true;
                                        }
                                        RadioButtionListvisibility(frmweb, WFTaskType, false, "", Convert.ToInt32(WFCurrentOrder));
                                    }
                                }
                            }
                        }
                    }
                    ReLoadDetails();
                    //ControlsReadOnly();
                });
            }
            catch (Exception ex)
            {
                ErrorMsgs = "TTrig_AMJ_IT_Forms :: E-Mail_ID_Request_Form" + " :: " + "Parent" + " :: " + "FormApprovalLoad";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }


        /// <summary>
        /// Function to bind data after the Form gets submitted.
        /// </summary>
        /// <param name="SiteURL"></param>

        #region "Reload Forms"

        public void ReLoadDetails()
        {
            try
            {
                camlQry = "<Where><Eq><FieldRef Name=\"Reference_x0020_Number\" /><Value Type=\"Text\">" + RefNo + "</Value></Eq></Where>";
                SPListItem lstDatas = obj.GetSPListItem(SiteURL, MainLstName, camlQry);
                if (lstDatas != null)
                {
                    lblEmailReqNo.Text = Convert.ToString(lstDatas["Reference_x0020_Number"]);
                    dtpDate.SelectedDate = Convert.ToDateTime(lstDatas["RequestDate"]);

                    pnlMailCreateInfo.Visible = true;

                    if (Request.QueryString["Mode"] == "View")
                    {
                        txtUserName.Text = Convert.ToString(lstDatas["UserName"]); txtUserName.ReadOnly = true;

                        if (string.IsNullOrEmpty(txtUserName.Text))
                            pnlMailCreateInfo.Visible = false;
                        txtemailAdd.Text = Convert.ToString(lstDatas["EmailAddress"]); txtemailAdd.ReadOnly = true;
                        txtPassView.Visible = false; txtPassWord.Visible = false;
                        txtViewPass.Text = Convert.ToString(lstDatas["Password"]); txtViewPass.Visible = true; txtViewPass.ReadOnly = true;
                        ckPassWord.Visible = false;
                    }


                    txtFirstName.Text = Convert.ToString(lstDatas["FirstName"]); txtFirstName.ReadOnly = true;
                    txtLastName.Text = Convert.ToString(lstDatas["LastName"]); txtLastName.ReadOnly = true;
                    ddlDivision.Items.FindByText(Convert.ToString(lstDatas["Division"])).Selected = true; ddlDivision.Enabled = false;

                    ReqEmpDivision = ddlDivision.SelectedItem.Text;
                    SPSecurity.RunWithElevatedPrivileges(delegate() { demandVoucherclass.GetDepartmentMaster(SiteURL, ddlDepartment, ReqEmpDivision); });

                    //ddlDepartment.SelectedItem.Text =Convert.ToString(lstDatas["Department"]); ddlDepartment.Enabled = false;
                    ddlDepartment.Items.FindByText(Convert.ToString(lstDatas["Department"])).Selected = true; ddlDepartment.Enabled = false;
                    txtMailUserMobNo.Text = Convert.ToString(lstDatas["MobileNo"]); txtMailUserMobNo.ReadOnly = true;
                    txtUserNameSuggestion.Text = Convert.ToString(lstDatas["UserNameSuggestion"]); txtUserNameSuggestion.ReadOnly = true;

                    if (Request.QueryString["Mode"] == "Edit")
                    {
                        txtUserName.Text = Convert.ToString(lstDatas["UserName"]);
                        txtemailAdd.Text = Convert.ToString(lstDatas["EmailAddress"]);
                        txtPassWord.Text = Convert.ToString(lstDatas["Password"]);
                        txtPassView.Text = Convert.ToString(lstDatas["Password"]);
                    }

                    if (!string.IsNullOrEmpty(txtUserName.Text))
                    {
                        txtUserName.ReadOnly = true; txtemailAdd.ReadOnly = true; txtPassWord.ReadOnly = true;
                    }

                    Label lblFormID = userInfo.FindControl("lblFormID") as Label;
                    lblFormID.Text = lstDatas.ParentList.ID.ToString();

                    Label lblreferenceNumber = emailComments.FindControl("lblReferenceNumber") as Label;
                    Label lblSiteUrl = emailComments.FindControl("lblSiteUrl") as Label;
                    Label lblfrmListName = emailComments.FindControl("lblfrmListName") as Label;
                    Label lbldocumentypesList = emailComments.FindControl("lbldocumentTypes") as Label;
                    Label lblCommentsList = emailComments.FindControl("lblCommentsList") as Label;
                    Label lblTaskList = emailComments.FindControl("lblTaskList") as Label;

                    lblTaskList.Text = TaskLstName;
                    lblCommentsList.Text = CmntsLstName;
                    lblfrmListName.Text = MainLstName;
                    lbldocumentypesList.Text = DocumentTypesList;
                    lblreferenceNumber.Text = lblEmailReqNo.Text;
                    lblSiteUrl.Text = SiteURL;

                }
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_IT_Forms :: E-Mail ID Request" + " :: " + "Parent" + " :: " + "ReLoadDetails";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }

        #endregion


        /// <summary>
        /// Radio button visibility for dynamic users
        /// </summary>
        /// <param name="tasktype"></param>
        /// <param name="optional"></param>
        /// <param name="OptionalUserName"></param>

        public void RadioButtionListvisibility(SPWeb oweb, string tasktype, bool optional, string OptionalUserName, int WFOrder)
        {
            try
            {
                if (Request.QueryString["Mode"] == "New")
                {
                    pnlForward.Visible = false;
                }
                else if (Request.QueryString["Mode"] == "Edit")
                {
                    if (tasktype == "Static")
                    {
                        pnlForward.Visible = false;
                    }
                    else if (tasktype == "Dynamic")
                    {
                        setRadioButtonVisibilityforDynamicType(WFOrder);
                    }
                    else if (tasktype == "UP Attribute")
                    {
                        pnlForward.Visible = false;
                    }
                    else if (tasktype == "EC (Externally Configured)")
                    {
                        pnlForward.Visible = false;
                    }

                    buttonVisibilty = demandVoucherclass.getForwardbuttonVisibility(SiteURL, WorkflowConfigList, MainLstName, WFOrder);
                    if (buttonVisibilty[3] == "True")
                    {
                        btnApprove.Visible = true;
                        btnReject.Visible = true;
                        btnSubmit.Visible = false;
                    }
                    else
                    {
                        btnApprove.Visible = false;
                        btnReject.Visible = false;
                        btnSubmit.Visible = true;
                    }
                    if (buttonVisibilty[4] == "False")
                    {
                        pnlCommentArea.Visible = false;
                    }

                }
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_IT_Forms" + " :: " + "Parent" + " :: " + "RadioButtionListvisibility";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }

        }

        /// <summary>
        /// set radion buttons visibilty for the dynamic work flow users
        /// </summary>
        /// <param name="WFOrder"></param>

        public void setRadioButtonVisibilityforDynamicType(int WFOrder)
        {
            try
            {
                buttonVisibilty = demandVoucherclass.getForwardbuttonVisibility(SiteURL, WorkflowConfigList, MainLstName, WFOrder);
                if (buttonVisibilty[0] == "True")
                {
                    rtbSendBack.Visible = true;
                    rtbSendBack.Checked = true;
                }
                else
                {
                    rtbSendBack.Visible = false;
                }
                if (buttonVisibilty[1] == "True")
                {
                    rtbForwardOthers.Visible = true;
                }
                else
                {
                    rtbForwardOthers.Visible = false;
                }

                if (buttonVisibilty[2] == "True")
                {
                    rtbForward.Visible = true;
                }
                else
                {
                    rtbForward.Visible = false;
                }
                if (buttonVisibilty[0] == "False" && buttonVisibilty[1] == "False" && buttonVisibilty[2] == "False")
                {
                    pnlForward.Visible = false;
                }
            }
            catch (Exception ex)
            {
                ErrorMsgs = "Trig_AMJ_IT_Forms :: E-Mail_ID_Request_Form" + " :: " + "Parent" + " :: " + "setRadioButtonVisibilityforDynamicType";
                obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
            }
        }

        void btnApprove_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                string formID = null; // Request.QueryString["FormID"];
                string refNo = Request.QueryString["RefID"];
                string taskItemID = Request.QueryString["TaskItemID"];
                string Approver = SPContext.Current.Web.CurrentUser.LoginName;
                if (GetSendbackStatus())
                {
                    if (CheckServersideValidation())
                    {
                        SPLongOperation lop = new SPLongOperation(this.Page);
                        lop.LeadingHTML = "<DIV><H4>E-Mail Request form approval is in progress</H4></DIV>";
                        lop.TrailingHTML = "<DIV><H5>Please wait...</H5></DIV>";
                        lop.Begin();
                        formID = obj.ApprovetheForm(SiteURL, MainLstName, TaskLstName, WorkflowConfigList, CmntsLstName, AttLibName, formID, refNo, taskItemID, WFTaskType, Approver,
                            TxtComments.Text, upfile, rtbForwardOthers, rtbSendBack, pplcnt, false);
                        obj.CreateEmailAccounts(SiteURL, refNo, formID, txtUserName.Text, txtemailAdd.Text, txtPassWord.Text);
                        lop.End(SPContext.Current.Site.Url, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, "");
                    }
                }
                else
                {

                    //lblError.Text = "Can't be send back. User is not available";
                    SPLongOperation lop = new SPLongOperation(this.Page);
                    lop.LeadingHTML = "<DIV><H4>E-Mail Request form approval is in progress</H4></DIV>";
                    lop.TrailingHTML = "<DIV><H5>Please wait...</H5></DIV>";
                    lop.Begin();
                    obj.ApprovetheForm(SiteURL, MainLstName, TaskLstName, WorkflowConfigList, CmntsLstName, AttLibName, formID, refNo, taskItemID, WFTaskType, Approver,
                        TxtComments.Text, upfile, rtbForwardOthers, rtbSendBack, pplcnt, true);
                    lop.End(SPContext.Current.Site.Url, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, "");
                }
            }
        }


        //private void UpdateEmailDatas()
        //{
        //    using (SPSite oSite = new SPSite(SiteURL))
        //    {
        //        using (SPWeb oWeb = oSite.OpenWeb())
        //        {

        //            camlQry = "<Where><Eq><FieldRef Name=\"Reference_x0020_Number\" /><Value Type=\"Text\">" + RefNo + "</Value></Eq></Where>";
        //            SPListItem oItem = obj.GetSPListItem(SiteURL, MainLstName, camlQry);// oWeb.Lists.TryGetList(MainLstName).GetItems();

        //            if (oItem != null)
        //            {
        //                oWeb.AllowUnsafeUpdates = true;
        //                oItem.ListItems.Add();
        //                oItem["UserName"] = txtUserName.Text;
        //                oItem["EmailAddress"] = txtemailAdd.Text;
        //                oItem[" Password "] = txtPassWord.Text;
        //                oWeb.AllowUnsafeUpdates = false;

        //            }

        //        }
        //    }

        //}

        void btnReject_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                SPLongOperation lop = new SPLongOperation(this.Page);
                lop.LeadingHTML = "<DIV><H4>E-Mail Request form rejection in progress</H4></DIV>";
                lop.TrailingHTML = "<DIV><H5>Please wait...</H5></DIV>";
                lop.Begin();

                //string formID = null; //Request.QueryString["FormID"];
                string refNo = Request.QueryString["RefID"];
                string TaskItemID = Request.QueryString["TaskItemID"];

                try
                {
                    Label formID = userInfo.FindControl("lblFormID") as Label;

                    SPFieldUserValue user = obj.GetSubmittedUser(SiteURL, formID.Text.ToString().Trim(), refNo);

                    if (user.LookupValue == SPContext.Current.Web.CurrentUser.LoginName)
                    {
                        obj.RejectForUser(SiteURL, MainLstName, TaskLstName, CmntsLstName, 2, AttLibName, upfile, WorkflowConfigList, refNo, formID.Text.ToString().Trim(), TxtComments.Text, null, TaskItemID);
                    }
                    else
                    {
                        trigWFClass.RejectTask(SiteURL, Request.QueryString["TaskItemID"], TaskLstName, MainLstName, Request.QueryString["RefID"]);

                        Label lblFormID = userInfo.FindControl("lblFormID") as Label;
                        lstGuid = lblFormID.Text;
                        if (!string.IsNullOrEmpty(upfile.FileName))
                            obj.AddAttachement(SiteURL, AttLibName, refNo, lstGuid, upfile);
                        // Reject user and comments updation
                        string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;
                        obj.SetCommnets(SiteURL, CmntsLstName, MainLstName, refNo, lstGuid, CurrentUser, TxtComments.Text, 0);
                    }
                }
                catch (Exception ex)
                {
                    ErrorMsgs = "Trig_AMJ_IT_Forms :: E-Mail Request form" + " :: " + "Parent" + " :: " + "Reject";
                    obj.GenerateExceptionLog(ErrorMsgs, ex.Message, "");
                }


                lop.End(SPContext.Current.Site.Url, Microsoft.SharePoint.Utilities.SPRedirectFlags.Default, this.Context, "");

            }
        }

        void rtbForward_CheckedChanged(object sender, EventArgs e)
        {
            lblError.Text = "";
            lblWFStaticUser.Text = "If you press Submit/Approve the workflow will go to next step";
        }

        void rtbForwardOthers_CheckedChanged(object sender, EventArgs e)
        {
            lblWFStaticUser.Text = "";
            lblError.Text = "";
        }

        void rtbSendBack_CheckedChanged(object sender, EventArgs e)
        {
            lblError.Text = "";
            SPFieldUserValue ofldUser = demandVoucherclass.getSendBackUserfNametoAssignLabelText(SiteURL, TaskLstName, Convert.ToInt16(Request.Params["TaskItemID"]), lblEmailReqNo.Text, rtbSendBack, rtbForward, lblError);
            if (ofldUser != null)
            {
                lblWFStaticUser.Text = "Send back to " + ofldUser.User.Name;
                lblSendBacktoUser.Text = ofldUser.User.Name;
            }
        }

    }
}
